import {Component, Vue} from 'vue-property-decorator'
import { Action } from 'vuex-class'
import { errorMessage } from '@/utils'

@Component
export default class Captcha extends Vue {
  // Vuex
  @Action getCaptcha!: (payload?: any) => Promise<any>
  
  // 搜索
  public captcha: any = {
    key: '',
    img: '',
    label: '加载中'
  }
  
  /**
   * 刷新验证码
   *
   * @param {Object} [param] 参数
   * @return {Promise<any>}
   * @private
   */
  public refreshCaptcha(param?: any): Promise<any> {
    this.captcha.label = '加载中'
    return this.getCaptcha({param}).then((res: any) => {
      if (res.status === 200) {
        this.captcha.img = res.data.img
        this.captcha.key = res.data.key
      } else {
        this.captcha.img = ''
        this.captcha.label = '加载失败'
      }
      return res
    }).catch((err: any) => {
      this.captcha.label = '加载失败'
      errorMessage(err.message || '获取验证码失败')
    })
  }
}
